package com.gp.ssmplus.base.core.mybatis.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gp.ssmplus.base.core.mybatis.entity.EntityMap;

import java.io.Serializable;

/**
 * @author admin
 */

public class ResultBody<T> implements Serializable {
    private static final long serialVersionUID = -6190689122701100762L;

    /**
     * 响应编码
     */

    private int code = 100;
    /**
     * 提示消息
     */

    private String message;

    /**
     * 请求路径
     */

    private String path;

    /**
     * 响应数据
     */

    private T data;

    /**
     * http状态码
     */
    private int httpStatus;

    /**
     * 附加数据
     */

    private EntityMap extra = new EntityMap();

    /**
     * 响应时间
     */

    private long timestamp = System.currentTimeMillis();

    public ResultBody() {
        super();
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getPath() {
        return path;
    }

    public T getData() {
        return data;
    }

    public EntityMap getExtra() {
        return extra;
    }

    public ResultBody setExtra(EntityMap map) {
        this.extra = map;
        return this;
    }

    public long getTimestamp() {
        return timestamp;
    }

    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public int getHttpStatus() {
        return httpStatus;
    }

    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public boolean isOk() {
        return this.code == 100;
    }


    public static ResultBody ok() {
        return new ResultBody().code(100).msg("成功");
    }

    public static ResultBody ok(Object object) {
        if (object instanceof String) {
            return new ResultBody().code(100).msg(object.toString());
        } else {
            return new ResultBody().code(100).data(object);
        }
    }


    public static ResultBody result(Boolean tag) {
        if (tag) {
            return ok("操作成功", true);
        } else {
            return failed("操作失败");
        }
    }

    /**
     * @param tag    成功标记
     * @param action 动作,如授权,退回,审批
     * @return
     */
    public static ResultBody result(String action, Boolean tag) {
        if (tag) {
            return ok(action + "成功", true);
        } else {
            return failed(action + "失败");
        }
    }

    public static ResultBody ok(String msg, Object data) {
        return new ResultBody().code(100).msg(msg).data(data);
    }

    public static ResultBody failed() {
        return new ResultBody().code(101).msg("失败");
    }

    public static ResultBody failed(String msg) {
        return new ResultBody().code(101).msg(msg);
    }

    public static ResultBody failed(int code, String msg) {
        return new ResultBody().code(code).msg(msg);
    }

    public ResultBody code(int code) {
        this.code = code;
        return this;
    }

    public ResultBody msg(String message) {
        this.message = message;
        return this;
    }

    public ResultBody data(T data) {
        this.data = data;
        return this;
    }

    public ResultBody path(String path) {
        this.path = path;
        return this;
    }

    public ResultBody httpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
        return this;
    }

    public ResultBody put(String key, Object value) {
        if (this.extra == null) {
            this.extra = new EntityMap();
        }
        this.extra.put(key, value);
        return this;
    }

    @Override
    public String toString() {
        return "ResultBody{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", path='" + path + '\'' +
                ", data=" + data +
                ", httpStatus=" + httpStatus +
                ", extra=" + extra +
                ", timestamp=" + timestamp +
                '}';
    }




}
